from django.db import models
from utils.models import CoreModel, table_prefix


class Button(CoreModel):
    name = models.CharField(max_length=64, verbose_name="权限名称", help_text="权限名称")
    value = models.CharField(max_length=64, verbose_name="权限值", help_text="权限值")

    class Meta:
        db_table = table_prefix + "button"
        verbose_name = '权限标识表'
        verbose_name_plural = verbose_name
        ordering = ('-name',)


class Menu(CoreModel):
    parent = models.ForeignKey(to='self', to_field='id', on_delete=models.CASCADE, related_name='children',
                               verbose_name='上级菜单', null=True,
                               blank=True)
    name = models.CharField(max_length=128, verbose_name='路由名')
    path = models.CharField(max_length=255, verbose_name='路由', null=True, blank=True)
    redirect = models.CharField(max_length=255, verbose_name='重定向地址', null=True, blank=True)
    component = models.CharField(max_length=255, verbose_name='前端组件', null=True, blank=True)

    title = models.CharField(max_length=128, verbose_name='菜单标题|按钮名称')
    icon = models.CharField(max_length=128, verbose_name='图标', null=True, blank=True)
    show = models.BooleanField(default=True, verbose_name='是否为隐藏菜单')
    target = models.CharField(max_length=128, verbose_name='链接打开方式', null=True, blank=True)
    link = models.CharField(max_length=128, null=True, blank=True)
    permission = models.CharField(max_length=128, verbose_name='按钮权限标识', null=True, blank=True)
    hideChildren = models.BooleanField(default=False, verbose_name='是否隐藏子菜单')
    sort = models.IntegerField(default=100, verbose_name='排序')
    STATUS_CHOICES = (
        (0, '禁用'), (1, '正常'), (2, '删除')
    )
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    TYPE_CHOICES = (
        (0, '目录'), (1, '菜单'), (2, '按钮')
    )
    type = models.IntegerField(choices=TYPE_CHOICES, verbose_name='状态')
    api = models.CharField(max_length=200, verbose_name="接口地址", help_text="接口地址", null=True, blank=True)
    METHOD_CHOICES = (
        (0, "GET"),
        (1, "POST"),
        (2, "PUT"),
        (3, "DELETE"),
    )
    method = models.IntegerField(choices=METHOD_CHOICES, default=0, verbose_name="接口请求方法", null=True, blank=True,
                                 help_text="接口请求方法")

    class Meta:
        db_table = table_prefix + "menu"
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name
        ordering = ('-sort',)
